package com.zqs.takeredpackage.util;

import com.zqs.takeredpackage.entity.User;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserStoreUtil {

    //存储用户数据
    private static final Map<String, User> userStore = new ConcurrentHashMap<>();

    /**
     * 添加用户
     */
    public void addUser(User user) {
        userStore.put(user.getAccount(), user);
    }

    /**
     * 通过account获取用户
     */
    public User getUser(String account) {
        return userStore.get(account);
    }

    /**
     * 根据Id 获取用户
     */
    public User getUserById(String id) {
        for (User user : userStore.values()) {
            if (user.getId().equals(id)) {
                return user;
            }
        }
        return null;
    }

    /**
     * 判断是否存在用户
     */
    public boolean hasUser(String account) {
        return userStore.containsKey(account);
    }

    /**
     * 根据username判断是否存在相同的 username
     */
    public boolean hasUserByUsername(String username) {
        for (User user : userStore.values()) {
            if (user.getUsername().equals(username)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取用户列表
     */
    public ArrayList<User> getUserList() {
        return new ArrayList<>(userStore.values());
    }

    /**
     * 更新用户红包
     */
    public void updateUserRedPackage(String account, BigDecimal money) {
        User user = userStore.get(account);
        user.setMoney(user.getMoney().add(money));
        userStore.put(account, user);
    }
}
